﻿using Models;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;
namespace CommonIoC
{
    public class PersonZhuangBeiBagMediator : Mediator
    {

       

        [Inject]
        public PersonZhuangBeiBagView view { get; set; }
        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        public override void OnRegister()
        {

             dispatcher.AddListener( PersonInfoPanelEvent.ClickZhuangBei , onShowZhuangBeiPanel);
            dispatcher.AddListener(PersonOneZhuangBeiViewEvent.ClickBack , onShowZhuangBeiPanel);

          
            //  dispatcher.AddListener(PlayerGameState.CanMove_And_CanTouchWSAD, onCanMove_And_CanTouchWSAD);
            //  dispatcher.AddListener(TopButtonListEvent.ClickHelper, onShowHelperPanel );


            //   dispatcher.AddListener(TopButtonListEvent.ClickBack, onTopButtonListClickBack);

            AddBackButtonClickFunc();
            AddOneZhuangBeiClickFunc();
        }

        void LoadPlayerAttackData()
        {
           // Dictionary<string, GamePerson> allplayer = GameDataManager.Instance.GetPersonDic();
            GamePerson person = GameDataManager.Instance.GetGamePerson();


            view.textName.text = person.pbase.PlayerName;
            int zhuangBeiAttack = CommonHelper.GetZhuangBeiAttack();

            int zhuangBeiFangwei = CommonHelper.GetZhuangBeiFangWei();
            view.attack.text = string.Format("攻击:{0}", person.point.Attack + zhuangBeiAttack );
            view.fangwei.text = string.Format("防卫:{0}", person.point.FangYu + zhuangBeiFangwei );

        }

        

        IEnumerator LoadPlayerBag_SetButtonShow()
        {
            //重新初始化一遍，避免数据来回 导致按钮依然存在
            for (int i = 0; i < view.btn_oneZhuangBei.Count; i++)
            {
                view.btn_oneZhuangBei[i].gameObject.SetActive(false);
                Text show_text = view.btn_oneZhuangBei[i].transform.GetChild(0).GetComponent<Text>();

                show_text.gameObject.name = "inint_text";
            }

            yield return new WaitForSeconds(0.2f);
            LoadPlayerAttackData();

           // Dictionary<string, GamePerson>  allplayer = GameDataManager.Instance.GetPersonDic();

            GamePerson person = GameDataManager.Instance.GetGamePerson();



            if (  person.zb_bag.bag == null )
               yield return null;

            
            foreach(KeyValuePair< int ,PersonZhuangBeiItem>   kv    in   person.zb_bag.bag)
            {
                Text show_text = view.btn_oneZhuangBei[kv.Key].transform.GetChild(0).GetComponent<Text>();
                if (kv.Value.isZhuangBei)
                {
                    show_text.text = string.Format("{0}(已装备)", kv.Value.Name);
                }
                else
                {
                    show_text.text = kv.Value.Name;
                }
                show_text.gameObject.name = kv.Key.ToString();
            }
            yield return new WaitForSeconds(0.2f);

            int resultkey;
            for(int i = 0; i< view.btn_oneZhuangBei.Count; i++)
            {
                Text show_text = view.btn_oneZhuangBei[i].transform.GetChild(0).GetComponent<Text>();
                if (  int.TryParse( show_text.gameObject.name  , out resultkey  ))
                {
                    view.btn_oneZhuangBei[i].gameObject.SetActive(true);
                }
                else
                {
                    view.btn_oneZhuangBei[i].gameObject.SetActive(false);
                }
            }


            view.CloseThisUI();
            view.ShowThisUI();
        }

        private void onShowZhuangBeiPanel(IEvent payload)
        {
           StartCoroutine( LoadPlayerBag_SetButtonShow() );

            view.ShowThisUI();
            //读取装备的操作。。。
        }

        void AddOneZhuangBeiClickFunc()
        {
            for(int i = 0; i < view.btn_oneZhuangBei.Count; i++)
            {
                if (view.btn_oneZhuangBei[i] != null && view.btn_oneZhuangBei[i].IsActive())
                {

                    Text show_text = view.btn_oneZhuangBei[i].transform.GetChild(0).GetComponent<Text>();
                    view.btn_oneZhuangBei[i].onClick.AddListener(delegate ()
                    {

                        dispatcher.Dispatch(PersonZhuangBeiBagEvent.ClickOneItem , show_text.gameObject.name );
                        view.CloseThisUI();
                    });
                }
            }
        }


        void AddBackButtonClickFunc()
        {
            if (view.btn_back != null)
            {
                view.btn_back.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch(  PersonZhuangBeiBagEvent.ClickBack  );
                    view.CloseThisUI();
                });
            }
        }


        public override void OnRemove()
        {
            //dispatcher.RemoveListener(PlayerStageCommandEvent.canMove, onPlayerStageChangeTocanMove);
            //dispatcher.RemoveListener(PlayerStageCommandEvent.doTalk, onPlayerStageChangeTodoTalk);
            dispatcher.RemoveListener(PersonInfoPanelEvent.ClickZhuangBei, onShowZhuangBeiPanel);
            dispatcher.RemoveListener(PersonOneZhuangBeiViewEvent.ClickBack, onShowZhuangBeiPanel);

            //view.dispatcher.RemoveListener(GameEvent.InputKey, onInputKey);
        }


    }
}
